package edu.nyupoly.wzj;

public class Board {
	
	public Cell[][] cells;
	public int length;
	public int width;
	
	private Player human;
	private Player computer;
	private Player currentPlayer;
	
	private int currentRow;
	private int currentColumn;
	private int currentPieceMark;
	
	public Board(Player comp, Player hum) {
		
		this.computer = comp;
		this.human = hum;
		
		this.length = 3;
		this.width = 3;
		this.cells = new Cell[3][3];
		
		for(int i=0; i<3; i++){
			for(int j=0; j<3; j++){
				cells[i][j] = new Cell(i,j);
			}
		}
	}
	
	public Player playerToPlay() {
		return this.currentPlayer;
	}
	
	public void setCurrentPlayer(Player p) {
		this.currentPlayer = p;
	}
	
	public void changePlayer() {
		if(currentPlayer.equals(computer)) {
			currentPlayer = human;
		} else {
			currentPlayer = computer;
		}
	}
	
	public void setCurrentRow(int currentRow) {
		this.currentRow = currentRow;
	}
	
	public void setCurrentCol(int currentCol) {
		this.currentColumn = currentCol;
	}
	
	public void setPiece(int row, int col, Player p) {
		if(p.equals(currentPlayer)) {   // to guarantee the player is currentPlayer
			setCurrentRow(row);
			setCurrentCol(col);
			cells[row][col].claimed = true;
			cells[row][col].setOwner(p);
			cells[row][col].cellMark = p.myMark;
		}
	}
	
	public void setCurrentPieceMark(int currentPieceMark) {
		this.currentPieceMark = currentPieceMark;
	}
	
	public int getCurrentRow() {
		return this.currentRow;
	}
	
	public int getCurrentCol() {
		return this.currentColumn;
	}
	
	public int getCurrentPieceMark() {
		return this.currentPieceMark;
	}

	public void clearBoard(){
		for(Cell[] rowOfCells: cells)
			for(Cell c:rowOfCells)
				c.setEmpty();
	}
	
	public void showBoard(){

		for(Cell[] rowOfCells: cells)
		{
			for(Cell c:rowOfCells)
			{
				switch(c.cellMark) {
					case CROSS:
						System.out.print("X ");
						break;
					case NOUGHT:
						System.out.print("O ");
						break;
					default:
						System.out.print("_ ");
						break;
				}
			}
			System.out.println();
		}
		System.out.println();
	}
	
	public boolean isFull() {
		for(int i=0; i<cells.length; i++ ) {
			for(int j=0; j<cells[i].length; j++) {
				if(cells[i][j].cellMark == Mark.EMPTY){
					return false;
				}
			}
		}
		return true;
	}
	
	public boolean isTie() {
		return isFull();
	}
	
	
	
	/*
	 *  There are 4 cases that are winning cases
	 */
	public boolean hasWon(Mark mark) {
		return ( // in the same row
				(cells[currentRow][0].cellMark == mark 
				&& cells[currentRow][1].cellMark == mark 
				&& cells[currentRow][2].cellMark == mark ) 
				// in the same column
				||( cells[0][currentColumn].cellMark == mark 
				&& cells[1][currentColumn].cellMark == mark 
				&& cells[2][currentColumn].cellMark == mark )
				// in the diagonal
				||( currentColumn==currentRow 
				&& cells[0][0].cellMark == mark 
				&& cells[1][1].cellMark == mark 
				&& cells[2][2].cellMark == mark )
				// in the anti-diagonal
				||( currentColumn + currentRow == 2 
					&& cells[0][2].cellMark == mark 
					&& cells[1][1].cellMark == mark 
					&& cells[2][0].cellMark == mark )
		);
	}

	/*
	public static void main(String args[]) {
		Board bd = new Board();
		bd.showBoard();
		System.out.println(bd.hasWon(Mark.NOUGHT));
	}*/
}
